﻿using System;
using System.IO;
using System.Linq;
using System.Net;
using Tau.Common.Utilities;
using Tau.DataAccess;
using Tau.Service;
using Image = Tau.Model.Image;

namespace ConvertImages
{
    class Program
    {
        static void Main(string[] args)
        {
            var context = new TauContext();

            var svc = new TauService();

            //get all images from products
            Console.WriteLine("Getting list of all products...");
            var allProducts = context.tblItems.Where(item => !string.IsNullOrEmpty(item.itemImgLg) && (!item.imageID.HasValue || item.imageID <= 0));
            bool updatesHappened = false;

            foreach (var product in allProducts)
            {
                //only attempt to get image if this product does not yet have an imageid and has a large image defined
                if (!string.IsNullOrEmpty(product.itemImgLg) && (!product.imageID.HasValue || product.imageID <= 0))
                {
                    //first, see if this image is in the images table
                    var dbImage = context.tblImages.Where(img => img.fileName.ToLower() == product.itemImgLg.ToLower()).FirstOrDefault();
                    if (dbImage != null) //If this image already exists, assign the id to this product
                    {
                        product.imageID = dbImage.imageID;
                        updatesHappened = true;
                    }
                    else
                    {
                        Console.WriteLine(string.Format("Getting image {0} for product {1}", product.itemImgLg, product.itemName));

                        WebRequest requestPic = WebRequest.Create(string.Format("http://tau-publishing.com/uplImages/{0}", product.itemImgLg));
                        requestPic.Proxy = WebProxy.GetDefaultProxy();
                        requestPic.Proxy.Credentials = CredentialCache.DefaultCredentials;

                        WebResponse responsePic = requestPic.GetResponse();

                        if (responsePic.ContentLength > 0)
                        {
                            Console.WriteLine(string.Format("Importing image {0} for product {1}", product.itemImgLg, product.itemName));

                            var webImage = System.Drawing.Image.FromStream(responsePic.GetResponseStream());
                            byte[] theImage = ImageUtilities.ConvertImageToByteArray(webImage);

                            var newImage = new Image
                                               {
                                                   LargeImage = theImage,
                                                   FileName = product.itemImgLg,
                                                   Size = theImage.Length,
                                                   Category = "Other"
                                               };

                            newImage = svc.AddImage(newImage, 600, 150);

                            product.imageID = newImage.ImageId;

                            updatesHappened = true;
                        }
                    }
                }
            }

            if (updatesHappened)
            {
                Console.WriteLine("Saving all changes");
                context.SaveChanges();
            }
            
            Console.WriteLine("Update complete.  Press any key to exit.");
            Console.Read();
        }
    }
}
